package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.MaintenanceCostLogic;
import cn.topevery.garden.dal.logic.MaintenanceRecordLogic;
import cn.topevery.garden.entity.dto.MaintenanceCostDTO;
import cn.topevery.garden.entity.po.MaintenanceCost;
import cn.topevery.garden.entity.query.MaintenanceCostQuery;
import cn.topevery.garden.entity.vo.MaintenanceCostVO;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 保养费用记录
 *
 * @author code-generate
 * @date 2022-02-17
 */
@Api(tags = "保养费用记录")
@RestController
@RequestMapping("/maintenanceCost")
public class MaintenanceCostController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    MaintenanceCostLogic maintenanceCostLogic;

    @Autowired
    MaintenanceRecordLogic maintenanceRecordLogic;
    /**
     * 获取保养费用记录分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取保养费用记录页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<MaintenanceCostVO>> listPage(@RequestBody MaintenanceCostQuery query) {
        return JsonResult.ok(maintenanceCostLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 保养费用记录信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult saveOrUpdate(@Valid @RequestBody MaintenanceCostDTO dto) {
        MaintenanceCost maintenanceCost = mapper.map(dto, MaintenanceCost.class);
        Integer maintenanceRecordId = maintenanceRecordLogic.getId(dto.getRecordCode());
        if(maintenanceRecordId.equals(0)){
            return JsonResult.error("记录编号错误");
        }
        maintenanceCost.setMaintenanceId(maintenanceRecordId);
        maintenanceCostLogic.saveOrUpdate(getUser().id.toString(),maintenanceCost);
        return JsonResult.ok(true);
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<MaintenanceCostVO> getInfoById(@RequestParam Integer id) {
        MaintenanceCost entity = maintenanceCostLogic.getEntity(id);
        MaintenanceCostVO vo= mapper.map(entity, MaintenanceCostVO.class);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        maintenanceCostLogic.deleteBatchById(getUser().id.toString(),ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出保养费用记录信息")
    @GetMapping("/export")
    public void export(MaintenanceCostQuery query, HttpServletResponse response) {
        List<MaintenanceCostVO> list = maintenanceCostLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "保养费用记录", "sheet1", MaintenanceCostVO.class, "保养费用记录", response);
    }

}
